package ferp.android.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.TextView;
import ferp.android.GUI;
import ferp.android.R;
import ferp.android.managers.DeckManager;
import ferp.core.card.Card;
import ferp.core.game.Bid;
import ferp.core.log.Log;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public class BidView3 extends TextView implements DeckManager.RefreshableView
{
  public interface Reference
  {
    float getTextSize();
  }

  public BidView3(Context context) {this(context, null);}
  public BidView3(Context context, AttributeSet attrs)
  {
    super(context, attrs);

    setTypeface(GUI.Font.BOLD);

    // register for suit image changes
    if (!isInEditMode())
      DeckManager.instance(context).register(this);
    else
      show(Bid.play(Card.Suit.HEARTS, 7));
  }

  // set reference
  public void set(Reference reference) {this.reference = reference;}

  // show bid
  public void show(Bid bid) {show(bid, false);}
  public void show(Bid bid, boolean abbreviated)
  {
    // bid type might be null when confirmation dialog is restored even without being shown
    if (bid.type != null)
      if (bid.isPlay())
      {
        setTextColor(GUI.getBidColor(bid));
        setText(String.valueOf(bid.tricks));

        this.trump = bid.trump;
        refresh();
      }
      else
      {
        Resources r = getResources();
        String s = bid.isCheck10() ? r.getString(R.string.bid_mine) + " " + bid.tricks : r.getString(r.getIdentifier("input_" + bid.toString(), "string", getContext().getPackageName()));

        // show only first letter if abbreviated flag is set (this only happens in pool dialog)
        if (abbreviated)
          s = "" + s.charAt(0);

        // hide image
        setCompoundDrawables(null, null, null, null);
        setTextColor(GUI.Color.NORMAL);
        setText(s);

        trump = null;
      }
  }

  @Override
  public void refresh()
  {
    if (trump == null)
      return;

    Drawable d = isInEditMode() ? getResources().getDrawable(R.drawable.suit_h_e) : DeckManager.instance(getContext()).getSuitImage(trump, isEnabled());
    // todo: calculations if the following 2 lines may not work for other screen sizes!
    int s = Math.round((reference == null ? getTextSize() : reference.getTextSize()) * 0.80f);

    d.setBounds(2, 2, s, s);
    setCompoundDrawables(null, null, d, null);
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  {
    if (reference != null)
      setTextSize(TypedValue.COMPLEX_UNIT_PX, reference.getTextSize());

    refresh();

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    Log.debug(Log.TAG, "BV2 - ms=" + GUI.m2s(widthMeasureSpec, heightMeasureSpec) + ", mh=" + getMeasuredHeight() + ", ts=" + getTextSize());
  }

  private Reference reference;
  private Card.Suit trump;
}
